Skip to content

Conversation

@jssmith
Copy link
Contributor

@jssmith jssmith commented Jan 22, 2026

Summary

Adds optional CORS support for the SSE transport, allowing browser-based MCP clients (like the Claude.ai Chrome extension) to connect to locally-running postgres-mcp servers.

This PR is based on the excellent work by @pbeast in #87. Their implementation provided the foundation - this PR adds fixes and test coverage.

Changes from original PR #87:

  • Fixed README to use correct argument name (--cors-origins not --cors-origin)
  • Fixed Docker example syntax (space not = for argparse nargs)
  • Removed unused import
  • Fixed import sorting and trailing whitespace
  • Added comprehensive pytest test coverage (8 tests)

Features:

  • Adds --cors-origins argument to whitelist allowed origins
  • Uses Starlette's CORSMiddleware on the SSE app
  • Works with multiple origins: --cors-origins https://claude.ai https://example.com

Usage

postgres-mcp --transport=sse --cors-origins https://claude.ai https://example.com

Test plan

  • CORS preflight requests return correct headers for allowed origins
  • CORS preflight requests reject non-allowed origins
  • Actual requests include CORS headers for allowed origins
  • No CORS headers when middleware not configured
  • All 157 unit tests pass

Fixes #73

🤖 Generated with Claude Code

pbeast and others added 3 commits June 24, 2025 17:30
- Fix README to use --cors-origins (plural) matching the actual CLI argument
- Fix Docker example to use correct argument syntax (space not =)
- Remove unused Middleware import
- Fix import sorting per ruff
- Remove trailing whitespace and extra blank lines

Co-Authored-By: Pavel Yankelevich <pbeast@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Tests cover:
- Preflight (OPTIONS) requests for allowed/disallowed origins
- Actual requests with CORS headers
- Behavior when CORS middleware is not configured

Co-Authored-By: Pavel Yankelevich <pbeast@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 046dc63499

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

starlette_app = mcp.sse_app()

if args.cors_origins:
logger.info(f"Enabling CORS for origins: {", ".join(args.cors_origins)}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Fix f-string syntax that breaks server startup

This logger.info line has an invalid f-string because the inner ", " string terminates the outer double-quoted f-string, which makes the module fail to parse at import time. In any environment that executes this code path (including postgres-mcp --transport=sse), Python will raise a SyntaxError before the server can start. Use single quotes inside the join or escape the quotes to keep the f-string valid.

Useful? React with 👍 / 👎.

jssmith and others added 2 commits January 22, 2026 15:49
Addresses code review feedback - while nested double quotes are valid
in Python 3.12+ (PEP 701), single quotes are clearer and avoid
potential linter confusion.

Co-Authored-By: Pavel Yankelevich <pbeast@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The unit tests verify the CORS middleware behavior but don't exercise
the actual server startup path. These e2e tests:
- Start a real uvicorn server in a subprocess
- Make actual HTTP requests with Origin headers
- Verify CORS headers are returned correctly

Co-Authored-By: Pavel Yankelevich <pbeast@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Expansion -- allow CORS whitelisting option for SSE protocol

3 participants